﻿namespace BEGDC.CC.Web.UserControls.CR
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using BEGDC.CC.BL.CCFlow.CR;
    using BEGDC.CC.Entity;
    using BEGDC.CC.BL.Workflow;
    using BEGDC.CC.BL.Common;
    using BEGDC.Utility.Castle;
    using BEGDC.CC.BL.Common.Query;

    /// <summary>
    ///		Summary description for DeptVPApprove.
    /// </summary>
    public class DeptDirectorApproval : BaseCR
    {
        #region Variables
        protected System.Web.UI.WebControls.Label lbAppNo;
        protected System.Web.UI.WebControls.Button Btn_Submit;
        protected System.Web.UI.WebControls.Button Btn_Query;
        protected System.Web.UI.WebControls.Button Btn_Reject;
        protected System.Web.UI.WebControls.Button Btn_Reply;
        protected BEGDC.CC.Web.UserControls.CR.NormalCtrl NormalCtrl;
        protected BEGDC.CC.Web.UserControls.CR.PrintCtrl PrintCtrl;
        protected System.Web.UI.WebControls.TextBox tbOperationFlag;
        protected System.Web.UI.WebControls.TextBox tbOperationContent;
        protected BEGDC.CC.Web.UserControls.CR.EZCtrl EZCtrl;
        protected System.Web.UI.WebControls.TextBox tbComment;
        protected BEGDC.CC.Web.Common.StampCtrl StampCtrl1;
        #endregion

        private bool IsQuery
        {
            get
            {
                string state = this.Request.QueryString["State"];
                if (state == null) return false;
                return state == SysDicItem.ProjectStatusFlag.QUERY;
            }
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!this.IsPostBack)
            {
                base.PreInitForm();

                this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
                this.Btn_Query.Attributes.Add("onclick", "javascript:OpenQueryPane(" + Request.QueryString["AppID"] + ");");
                this.Btn_Reject.Attributes.Add("onclick", "javascript:OpenCommentPane();");
                this.Btn_Reply.Attributes.Add("onclick", "javascript:OpenReplyPane(" + Request.QueryString["FlowID"] + ");");
                ButtonStatusSetting();

                InitForm();
                //SetStamp();

                //Check Access
                //Accessibility access = this.CheckAccessibility(WorkTransition.CR_DeptDirectorApproval, BEGDC.CC.BL.Workflow.UserRole.DeptDirector);
                //ControlStatusSetting( access );
            }
        }

        #region SetStamp
        //		private void SetStamp()
        //		{
        //			IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
        //			DataTable table = queryService.QueryActionLog( this.AppID );
        //			StampCtrl1.SetDataSource( table );
        //		}
        #endregion

        #region ControlStatusSetting
        private void ControlStatusSetting(Accessibility access)
        {
            if (access == Accessibility.Readonly)
            {
                this.DisableChildrenControls(this);
                //this.stampCtrl1.Visible = false;
            }
        }
        #endregion

        #region InitForm
        private void InitForm()
        {
            bool isReadOnly = true;
            long flowId = this.LoadApplication();
            if (this.Application.AppNo != string.Empty)
                this.lbAppNo.Text = " Application No:" + this.Application.AppNo;

            if (this.Application != null)
            {
                //decide the application type whether EZ or normal
                if (!this.Application.IsTransition)
                {
                    EZCtrl.Visible = false;
                    //PrintCtrl.Visible = false;
                    NormalCtrl.Visible = true;

                    NormalCtrl.isReadOnly = isReadOnly;
                    NormalCtrl.App = this.Application;
                }
                else
                {
                    EZCtrl.Visible = true;
                    NormalCtrl.Visible = false;

                    EZCtrl.isReadOnly = isReadOnly;
                    //this.Btn_Export.Visible = true;
                    //EZCtrl.Application = this.Application;
                    //EZCtrl.LoadCtrl();
                }
            }
            else
            {
                WebUtility.Go2ErrorPage(this.Page, "wrong Application ID");
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Reply.Click += new EventHandler(Btn_Reply_Click);
            this.Btn_Query.Click += new EventHandler(Btn_Query_Click);
            this.Btn_Reject.Click += new EventHandler(Btn_Reject_Click);
            this.Btn_Submit.Click += new EventHandler(Btn_Submit_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region BuildObjects

        private BEGDC.CC.Entity.Application BuildApplication()
        {
            //			ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
            //			BEGDC.CC.Entity.Application App = this.Application;
            //			if(App==null)
            //			{
            //				App = new BEGDC.CC.Entity.Application();
            //			}
            //
            //			App.AppNo = "CR Test App";
            //			App.RequesterNameEN = this.Ctl_ApplicantName.Text;
            //			App.RequesterOfficeTel = this.Ctl_Cellphone.Text;
            //			App.RequesterMobile = this.Ctl_Cellphone.Text;
            // 
            //			return App;
            ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
            BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);
            return NormalCtrl.BuildApplication(App);
        }

        #endregion

        #region Submit
        void Btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                BEGDC.CC.Entity.Application App = BuildApplication();
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;

                CRService.SubmitDeptDirectorApproval(App, this.tbComment.Text);

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                //throw exp;
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            }
        }
        #endregion

        #region Reject
        void Btn_Reject_Click(object sender, EventArgs e)
        {
            try
            {
                BEGDC.CC.Entity.Application App = BuildApplication();
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                CRService.RejectDeptDirectorApproval(Convert.ToInt64(Request.QueryString["FlowID"]), WorkTransition.Reject, this.tbComment.Text, true);

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                //throw exp;
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            }
        }
        #endregion

        #region Query
        void Btn_Query_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.tbOperationFlag.Text == "query")
                {
                    ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                    CRService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());

                    Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
                }
            }
            catch (Exception exp)
            {
                //throw exp;
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            }
        }
        #endregion

        #region Export
        private void Btn_Export_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                ExcelTool.ICCExcelExport excelExport;
                if (this.Application.IsTransition)
                    excelExport = new ExcelTool.ExportCRFormA();
                else
                    excelExport = new ExcelTool.ExportCRFormB();
                excelExport.Export(this.Page, this.AppID);
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");

            }
            catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
            }
        }
        #endregion

        #region ButtonStatusSetting
        protected override void ButtonStatusSetting()
        {
            if (this.Access == null)
                return;

            this.Btn_Submit.Visible = this.Access.IsButtonVisible(CRTransitionList.Approve);
            this.Btn_Query.Visible = this.Access.IsButtonVisible(CRTransitionList.Query);
            //this.Btn_Reject.Visible = this.Access.IsButtonVisible(CRTransitionList.Reject);
            this.Btn_Reject.Visible = false;
            this.Btn_Reply.Visible = this.Access.IsButtonVisible(CRTransitionList.Reply);
        }
        #endregion

        #region Reply
        void Btn_Reply_Click(object sender, EventArgs e)
        {
            try
            {
                BEGDC.CC.Entity.Application App = BuildApplication();
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;

                CRService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));

                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
            }
            catch (Exception exp)
            {
                //throw exp;
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            }
        }
        #endregion
    }
}
